
import {EnvConfig} from "@/utils/index";

export default class WebSocketClient {

    private ws: WebSocket = null;

    private ipAddress: string = '';

    private wsTimeout: any = null;

    private heartbeatInterval = null;

    private baseUrl: string = '';

    private callback = null;

    private emplId: string = null;

    private scheduleId: string = null;

    private roomId: string = null;

    public constructor(baseUrl, ip, callback) {
        this.baseUrl = baseUrl ? this.formatWsUrl(baseUrl) : this.baseUrl
        this.ipAddress = ip
        this.callback = callback
        this.init()
    }


    public init(): void {
        const that = this
        if (that.ws === null) {
            that.ws = new WebSocket(this.baseUrl + "/socket/connect?wsId="+this.ipAddress+"&app_secret=123")
        }
        const timeout = setTimeout(function() {
            that.ws.close();
        }, 10000); // 设置超时时间为5秒

        that.ws.onopen = function () {
            clearTimeout(timeout)
            console.log('================websocket open================', that.baseUrl)
            that.sendHeartbeat();
        }
        that.ws.onmessage = function (event) {
            console.log('================websocket onmessage================', event.data)
            if (that.callback) {
                that.callback(1, JSON.parse(event.data))
            }
        }
        that.ws.onerror = function (event) {
            that.ws = null
            if (that.callback) {
                that.callback(0, null)
            }
            clearTimeout(timeout)
            if (that.wsTimeout) {
                clearTimeout(that.wsTimeout)
                that.wsTimeout = null
            }
            console.log('================websocket onerror================', that.baseUrl)
            that.wsTimeout = setTimeout(function () {
                that.init()
            }, 15000)
        }
        that.ws.onclose = function () {
            if (that.callback) {
                that.callback(0, null)
            }
            clearTimeout(timeout)
            if (that.wsTimeout) {
                clearTimeout(that.wsTimeout)
                that.wsTimeout = null
            }
            that.ws = null;
            that.wsTimeout = setTimeout(function () {
                that.init()
            }, 15000)
            console.log('=============websocket onclose-------------', that.baseUrl)
        }
    }

    public updateScheduleData(emplId, scheduleId, roomId): void {
        this.emplId = emplId
        this.scheduleId = scheduleId
        this.roomId = roomId
    }

    private sendHeartbeat() {
        const that = this
        if (this.ws == null) {
            return
        }
        this.ws.send(JSON.stringify({
            action: 'heartbeat',
            data: {
                emplId: that.emplId,
                scheduleId: that.scheduleId,
                roomId: that.roomId
            }
        }))
        setTimeout(function () {
            that.sendHeartbeat()
        }, 10000)
    }

    private formatWsUrl(url: string): string {
        if (url && url.indexOf("/dev-api") === 0) {
            return ""
        }
        return url && url.indexOf("http") === 0 ? "ws" + url.substring(url.indexOf("://")) : url;
    }
}